Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
The vinyl npm package is a virtual file format used in Node.js streams. It is commonly used in build systems like Gulp to represent files, allowing for easy manipulation and transformation of file contents and metadata.
Creating a Vinyl File
This feature allows you to create a new Vinyl file object. The code sample demonstrates how to create a Vinyl file with a specified current working directory, base path, file path, and contents.
const Vinyl = require('vinyl');
const file = new Vinyl({
cwd: "/",
base: "/test/",
path: "/test/file.js",
contents: Buffer.from("console.log('Hello, world!');")
});
console.log(file);
Reading File Contents
This feature allows you to read the contents of a Vinyl file. The code sample demonstrates how to create a Vinyl file and then read its contents as a string.
const Vinyl = require('vinyl');
const file = new Vinyl({
cwd: "/",
base: "/test/",
path: "/test/file.js",
contents: Buffer.from("console.log('Hello, world!');")
});
console.log(file.contents.toString());
Checking File Properties
This feature allows you to check various properties of a Vinyl file, such as whether its contents are a buffer, a stream, or null. The code sample demonstrates how to create a Vinyl file and check these properties.
const Vinyl = require('vinyl');
const file = new Vinyl({
cwd: "/",
base: "/test/",
path: "/test/file.js",
contents: Buffer.from("console.log('Hello, world!');")
});
console.log(file.isBuffer());
console.log(file.isStream());
console.log(file.isNull());
Gulp is a toolkit for automating painful or time-consuming tasks in your development workflow. It uses Vinyl as its file format, but provides a higher-level API for defining tasks and managing file streams. Compared to Vinyl, Gulp offers more built-in functionality for task automation.
Through2 is a small wrapper around Node.js streams2 Transform to avoid explicit subclassing noise. It is often used in conjunction with Vinyl to create transform streams that can manipulate file contents. While Through2 does not provide a file format like Vinyl, it complements Vinyl by offering a simple way to create transform streams.
Vinyl-fs is a file system adapter for Vinyl. It provides methods to read from the file system and write to it, using Vinyl file objects. Compared to Vinyl, vinyl-fs extends its functionality by integrating with the file system, making it easier to read and write files as Vinyl objects.
Package | vinyl |
Description | A virtual file format |
Node Version | >= 0.9 |
Read this for more info about how this plays into the grand scheme of things https://medium.com/@eschoff/3828e8126466
var File = require('vinyl');
var coffeeFile = new File({
cwd: "/",
base: "/test/",
path: "/test/file.coffee",
contents: new Buffer("test = 123")
});
When checking if an object is a vinyl file, you should not use instanceof. Use the isVinyl function instead.
var File = require('vinyl');
var dummy = new File({stuff});
var notAFile = {};
File.isVinyl(dummy); // true
File.isVinyl(notAFile); // false
Vinyl checks if a property is not managed internally, such as sourceMap
. This is than used in constructor(options)
when setting, and clone()
when copying properties.
var File = require('vinyl');
File.isCustomProp('sourceMap'); // true
File.isCustomProp('path'); // false -> internal getter/setter
Read more in Extending Vinyl.
Type: String
Default: process.cwd()
Used for relative pathing. Typically where a glob starts.
Type: String
Default: options.cwd
Full path to the file.
Type: String
Default: undefined
Path history. Has no effect if options.path
is passed.
Type: Array
Default: options.path ? [options.path] : []
The result of an fs.stat call. See fs.Stats for more information.
Type: fs.Stats
Default: null
File contents.
Type: Buffer, Stream, or null
Default: null
Any other option properties will just be assigned to the new File object.
var File = require('vinyl');
var file = new File({foo: 'bar'});
file.foo === 'bar'; // true
Returns true if file.contents is a Buffer.
Returns true if file.contents is a Stream.
Returns true if file.contents is null.
Returns a new File object with all attributes cloned.
By default custom attributes are deep-cloned.
If opt or opt.deep is false, custom attributes will not be deep-cloned.
If opt.contents is false, it will copy file.contents Buffer's reference.
If file.contents is a Buffer, it will write it to the stream.
If file.contents is a Stream, it will pipe it to the stream.
If file.contents is null, it will do nothing.
If opt.end is false, the destination stream will not be ended (same as node core).
Returns the stream.
Returns a pretty String interpretation of the File. Useful for console.log.
The Stream or Buffer of the file as it was passed in via options, or as the result of modification.
For example:
if (file.isBuffer()) {
console.log(file.contents.toString()); // logs out the string of contents
}
Absolute pathname string or undefined
. Setting to a different value pushes the old value to history
.
Array of path
values the file object has had, from history[0]
(original) through history[history.length - 1]
(current). history
and its elements should normally be treated as read-only and only altered indirectly by setting path
.
Returns path.relative for the file base and file path.
Example:
var file = new File({
cwd: "/",
base: "/test/",
path: "/test/file.coffee"
});
console.log(file.relative); // file.coffee
Gets and sets path.dirname for the file path.
Example:
var file = new File({
cwd: "/",
base: "/test/",
path: "/test/file.coffee"
});
console.log(file.dirname); // /test
file.dirname = '/specs';
console.log(file.dirname); // /specs
console.log(file.path); // /specs/file.coffee
Gets and sets path.basename for the file path.
Example:
var file = new File({
cwd: "/",
base: "/test/",
path: "/test/file.coffee"
});
console.log(file.basename); // file.coffee
file.basename = 'file.js';
console.log(file.basename); // file.js
console.log(file.path); // /test/file.js
Gets and sets stem (filename without suffix) for the file path.
Example:
var file = new File({
cwd: "/",
base: "/test/",
path: "/test/file.coffee"
});
console.log(file.stem); // file
file.stem = 'foo';
console.log(file.stem); // foo
console.log(file.path); // /test/foo.coffee
Gets and sets path.extname for the file path.
Example:
var file = new File({
cwd: "/",
base: "/test/",
path: "/test/file.coffee"
});
console.log(file.extname); // .coffee
file.extname = '.js';
console.log(file.extname); // .js
console.log(file.path); // /test/file.js
When extending Vinyl into your own class with extra features, you need to think about a few things.
When you have your own properties that are managed internally, you need to extend the static isCustomProp
method to return false
when one of these properties is queried.
const File = require('vinyl');
const builtInProps = ['foo', '_foo'];
class SuperFile extends File {
constructor(options) {
super(options);
this._foo = 'example internal read-only value';
}
get foo() {
return this._foo;
}
static isCustomProp(name) {
return super.isCustomProp(name) && builtInProps.indexOf(name) === -1;
}
}
This makes properties foo
and _foo
ignored when cloning, and when passed in options to constructor(options)
so they don't get assigned to the new object.
Same goes for clone()
. If you have your own internal stuff that needs special handling during cloning, you should extend it to do so.
FAQs
Virtual file format.
We found that vinyl demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.